package com.canbot.u05.utils;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;

import java.util.List;

/**
 * Created by Administrator on 2017/3/17.
 */

public class XunFeiYuYingUtils {

        private static XunFeiYuYingUtils xunFeiYuYingUtils;

        private Context mContext;

        private SpeechRecognizer mIat;

        //1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener
        public static XunFeiYuYingUtils getInstance() {
                if (xunFeiYuYingUtils == null) {
                        xunFeiYuYingUtils = new XunFeiYuYingUtils();
                }
                return xunFeiYuYingUtils;
        }

        private XunFeiYuYingUtils() {
        }

        public void init(Context context) {
                this.mContext = context;
                mIat = SpeechRecognizer.createRecognizer(mContext, null);
//2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
                mIat.setParameter(SpeechConstant.DOMAIN, "iat");
                mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
                mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
        }

        public void startRecognize(ResultListenr resultListenr) {
                this.mResultListenr = resultListenr;
                if (mIat == null) {
                        init(mContext);
                }
                mIat.startListening(mRecoListener);
        }

        public void stopRecognize() {
                mIat.stopListening();
        }

        private ResultListenr mResultListenr;

        public interface ResultListenr {

                void onResult(String result);

                void onError(int result);

                void onVolumeChanged(int i);

                void onBeginOfSpeech();

                void onEndOfSpeech();
        }

        //3.开始听写
//听写监听器
        private RecognizerListener mRecoListener = new RecognizerListener() {
                //听写结果回调接口(返回Json格式结果，用户可参见附录12.1)；
//一般情况下会通过onResults接口多次返回结果，完整的识别内容是多次结果的累加；
//关于解析Json的代码可参见MscDemo中JsonParser类；
//isLast等于true时会话结束。
                public void onResult(RecognizerResult results, boolean isLast) {
                        Log.d("Result:", results.getResultString());
                        String resultString = results.getResultString();
                        StringBuffer stringBuffer = new StringBuffer();
                        if (!TextUtils.isEmpty(resultString)) {
                                XunFeiResultbean xunFeiResultbean = new Gson().fromJson(resultString, XunFeiResultbean.class);
                                List<XunFeiResultbean.WsBean> ws = xunFeiResultbean.getWs();
                                for (XunFeiResultbean.WsBean w : ws) {
                                        for (XunFeiResultbean.WsBean.CwBean cwBean : w.getCw()) {
                                                String w1 = cwBean.getW();
                                                stringBuffer.append(w1);
                                        }
                                }
                        }
                        if (mResultListenr != null) {
                                mResultListenr.onResult(stringBuffer.toString());
                        }
                }

                //会话发生错误回调接口
                public void onError(SpeechError error) {
                        error.getPlainDescription(true); //获取错误码描述
                        if (mResultListenr != null) {
                                mResultListenr.onError(error.getErrorCode());
                        }
                }

                @Override
                public void onVolumeChanged(int i, byte[] bytes) {
                        if (mResultListenr != null) {
                                mResultListenr.onVolumeChanged(i);
                        }
                }

                //开始录音
                public void onBeginOfSpeech() {
                        if (mResultListenr != null) {
                                mResultListenr.onBeginOfSpeech();
                        }
                }

                //结束录音
                public void onEndOfSpeech() {
                        if (mResultListenr != null) {
                                mResultListenr.onEndOfSpeech();
                        }
                }

                //扩展用接口
                public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {

                }
        };
}
